package org.liukai.ssh_ems.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.liukai.ssh_ems.entity.po.Emp;
import org.liukai.ssh_ems.service.EmpService;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @create EmpAction.java 15:06
 * 
 * @author liukai
 * @date 2016/1/8
 * @detail 
 */
@Controller
public class EmpAction extends ActionSupport {
    @Resource
    private EmpService empService;

    private Emp emp;



    //测试方法
    public String sayHello() {
        return SUCCESS;
    }

    //查所有
    public String queryEmpList() {
        //获得ActionContext提供的request map对象
        Map<String,Object> request = (Map)ActionContext.getContext().get("request");
        List<Emp> list = empService.queryEmpList();
        request.put("empList", list);
//        for (Emp e : list) {
//            System.out.println(e);
//        }
        return SUCCESS;
    }

    //跳转到添加页面
    public String addEmpView() throws Exception{
       /* if(true) {
            throw new Exception();
        }*/
        return SUCCESS;
    }

    //添加emp
    public String addEmp() {
        empService.insertEmp(emp);
        return SUCCESS;
    }

    //删除emp
    public String deleteEmp() {
        empService.deleteEmp(emp.getEmpId());
        return SUCCESS;
    }

    //跳转修改页面
    public String updateEmpView() {
        Map<String,Object> request = (Map)ActionContext.getContext().get("request");
        Emp e = empService.queryEmpById(emp.getEmpId());
        request.put("emp", e);
        return SUCCESS;
    }

    //修改emp
    public String updateEmp() {
        empService.updateEmp(emp);
        return SUCCESS;
    }




    public Emp getEmp() {
        return emp;
    }

    public void setEmp(Emp emp) {
        this.emp = emp;
    }

    public EmpAction() {
        System.out.println("实例化EmpAction");
    }
}
